1 Gráfico de barras Back-to-back

library(dplyr)
library(ggplot2)
library(plotly)
  • Criando uma base de dados fake:
set.seed(123)
ten_positive_rand_numbers <- abs(rnorm(10)) + 0.1
the_prob <- ten_positive_rand_numbers/sum(ten_positive_rand_numbers)

fk_data <- data.frame(job_type = sample(LETTERS[1:10],1000, replace = TRUE, prob = the_prob),
                      gender = sample(c("Male", "Female"), 1000, replace = TRUE)
                      )
  • Preparando base para o plot:
plotting_df <- fk_data %>% 
  group_by(job_type, gender) %>% 
  summarise(Freq = n()) %>% 
  # a trick!
  mutate(Freq = if_else(gender == "Male", -Freq, Freq))
## `summarise()` regrouping output by 'job_type' (override with `.groups` argument)
  • Encontrando a ordem:
temp_df <- plotting_df %>% 
  filter(gender == "Female") %>% 
  arrange(Freq)
the_order <- temp_df$job_type
  • Plot:
p <- plotting_df %>% 
  ggplot(aes(x = job_type, y = Freq, group = gender, fill= gender)) +
  geom_bar(stat = "identity", width = 0.75) +
  coord_flip() +
  scale_x_discrete(limits = the_order) +
  # another trick
  scale_y_continuous(breaks = seq(-150, 150, 50),
                     labels = abs(seq(-150, 150, 50))) +
  labs(x = "Job Type", y = "Count", title = "Back-to-back bar chart") +
  theme(legend.position = "bottom",
    legend.title = element_blank(),
    plot.title = element_text(hjust = 0.5),
    panel.background = element_rect(fill="grey90")
  )+
  scale_fill_manual(values = c("red", "blue"),
                    name = " ",
                    breaks = c("Male", "Female"),
                    labels = c("Male", "Female"))
ggplotly(p)
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.